import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path:"/login",
    component: () => import("../views/login.vue"),
  },
  {
    path:"/layout",
    component: () => import("../views/layout.vue"),
      children:[
        {
          path: "/index",
          component: () => import("../views/Index.vue"),
        },
        {
          path: "/menu",
          component: () => import("../views/menu.vue"),
        },
        {
          path: "/role",
          component: () => import("../views/role.vue"),
        },
        {
          path: "/user",
          component: () => import("../views/user.vue"),
        },
        {
          path: "/category",
          component: () => import("../views/cate.vue"),
        },
        {
          path: "/specs",
          component: () => import("../views/space.vue"),
        },
        {
          path: "/goods",
          component: () => import("../views/goods.vue"),
        },
        
      ]
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach(function(to,from,next){
  const no_login_list = ["/login"];
  if(no_login_list.indexOf(to.path) === -1){
    var tokenString = localStorage.getItem("login_data") || "{}";
    var token = JSON.parse(tokenString).token;
    if(token){
      var menu_list = JSON.parse(tokenString).menus_url
      menu_list.push("/index")
      menu_list.push("/login")
      if(menu_list.indexOf(to.path) === -1){
        //没有查到权限
        next("/index")
      }else{
        next()
      }
    }else{
      next("/login")
    }
  }else{
    next()
  }
})

export default router
